package com.kungkhies.jms.kungkhie.upload;

import com.kungkhies.common.services.kungkhie.validate.KungkhieValidateException;
import com.kungkhies.common.services.kungkhie.validate.KungkhieValidateService;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class KungkhieUploadHandler
{
    private JmsTemplate jmsTemplate;
    private KungkhieValidateService kungkhieValidateService;
    
    public KungkhieUploadHandler(JmsTemplate jmsTemplate, KungkhieValidateService kungkhieValidateService)
    {
        this.jmsTemplate = jmsTemplate;
        this.kungkhieValidateService = kungkhieValidateService;
    }
   
    /**
     * Extracts kungkhie string from JMS message and passes
     * to the validator.
     * 
     * On successful validation the kungkhie is placed in the 
     * <code>kungkhie.valid.queue</code> queue for persistence handling.
     * 
     * In the case of a validation error, the error is passed to the 
     * <code>kungkhie.validation.error.queue</code>  to be used in the
     * user's kspace callback..
     * 
     * @param message 
     */
    public void processKungkhieUpload(HashMap message)
    {
        String kungkherName = (String) message.get("kungkher");
        String kungkhieString = (String) message.get("kungkhie");

        System.out.println("Kungkher: " + kungkherName);

        System.out.println("Kungkhie: " + kungkhieString);

        if (isRegistered(kungkherName) == false)
        {
             //log security incident  
        }
        
        try
        {
            kungkhieValidateService.validateKungkhie(kungkhieString);
        }
        catch (KungkhieValidateException ex)
        {
            Logger.getLogger(KungkhieUploadHandler.class.getName()).log(Level.SEVERE, null, ex);
            
            enqueueValidateErrorMessage(ex.getMessage());
        }
        
        enqueueValidKungkhie(kungkhieString);;
        //upload processing completed successfully
    }

    public boolean isRegistered(String kungkherName)
    {
        return false;
    }
    
    
    
    
    public void enqueueValidateErrorMessage(final String errorMessage)
    {
        jmsTemplate.send("kungkhie.validate.error.queue", new MessageCreator()
        {
            @Override
            public Message createMessage(Session session) throws JMSException
            {
                Message mm = session.createTextMessage();
                
                mm.setStringProperty("kungkhieValidateErrorMessage", errorMessage);
               
                return mm;
            }
        });
    }
    
    public void enqueueValidKungkhie(final String kungkhieString)
    {
        jmsTemplate.send("kungkhie.valid.queue", new MessageCreator()
        {
            @Override
            public Message createMessage(Session session) throws JMSException
            {
                Message mm = session.createTextMessage(kungkhieString);
               
                return mm;
            }
        });    
    }
}
